/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;

import br.ufmg.dees.insane.model.geo.geometricTransformations.transformations.ScaleTransform;
import br.ufmg.dees.insane.model.geo.geometricTransformations.transformations.Translation;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;


/**
 *
 * @author  Samuel Silva Penna
 */
public class TranslationDialog extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Variables declaration - do not modify                     
	private JButton upButton;
	private JButton downButton;
	private JButton leftButton;
	private JButton rightButton;
	private JButton centerButton;
	private JButton closeButton;
	private JButton scaleButton;
	private JLabel jLabel1;
	private JPanel centerPanel;
	private DecimalField stepTextField;
	private DecimalField scaleTextField;
	private JSeparator jSeparator1;
	private JSeparator jSeparator2;
	// End of variables declaration                   
	
	private InterfacePostp parent;
	private Translation translation;
	
	private NumberFormat numberFormat;
	
	/** Creates new form TraslationDialog */
	public TranslationDialog(JInternalFrame parent) {
		this.parent = (InterfacePostp)parent;
		this.translation = new Translation(1,1,0);
		initComponents();
	}
	
//	/** Creates new form TraslationDialog */
//	public TranslationDialog() {
//	initComponents();
//	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
	private void initComponents() {
		this.setTitle("Move");
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		this.setSize(new Dimension(400,200));
		this.setMinimumSize(new Dimension(400,200));
		this.setMaximumSize(new Dimension(400,200));
		this.setPreferredSize(new Dimension(400,200));
		this.setResizable(false);
		this.setLocationRelativeTo(parent.getDesktop());
		this.setAlwaysOnTop(true);
		setUpFormat();
		
		upButton = new JButton();
		downButton = new JButton();
		leftButton = new JButton();
		rightButton = new JButton();
		centerPanel = new javax.swing.JPanel();
		centerButton = new JButton();
		closeButton = new JButton();
		scaleButton = new JButton();
		stepTextField = new DecimalField(1,10,numberFormat);
		scaleTextField = new DecimalField(1,10,numberFormat);
		jLabel1 = new javax.swing.JLabel();
		jSeparator1 = new JSeparator();
		jSeparator2 = new JSeparator();
		
		getContentPane().setLayout(null);
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		upButton.setText("Up");
		upButton.setAlignmentY(0.0F);
		upButton.setIconTextGap(0);
		upButton.setMargin(new java.awt.Insets(1, 1, 1, 1));
		getContentPane().add(upButton);
		upButton.setFont(new Font("Arial", 0, 12));
		upButton.setBounds(75, 5, 60, 30);
		
		downButton.setText("Down");
		downButton.setAlignmentY(0.0F);
		downButton.setIconTextGap(0);
		downButton.setMargin(new Insets(1, 1, 1, 1));
		getContentPane().add(downButton);
		downButton.setFont(new Font("Arial", 0, 12));
		downButton.setBounds(75, 85, 60, 30);
		
		leftButton.setText("Left");
		leftButton.setAlignmentY(0.0F);
		leftButton.setIconTextGap(0);
		leftButton.setMargin(new Insets(1, 1, 1, 1));
		getContentPane().add(leftButton);
		leftButton.setFont(new Font("Arial", 0, 12));
		leftButton.setBounds(5, 45, 60, 30);
		
		rightButton.setText("Right");
		rightButton.setAlignmentY(0.0F);
		rightButton.setIconTextGap(0);
		rightButton.setMargin(new Insets(1, 1, 1, 1));
		getContentPane().add(rightButton);
		rightButton.setFont(new Font("Arial", 0, 12));
		rightButton.setBounds(145, 45, 60, 30);
		
		centerPanel.setLayout(null);
		centerPanel.setBorder(BorderFactory.createLineBorder(new Color(204, 204, 204)));
		
		centerButton.setText("Center");
		centerButton.setAlignmentY(0.0F);
		centerButton.setIconTextGap(0);
		centerButton.setMargin(new Insets(1, 1, 1, 1));
		centerButton.setFont(new Font("Arial", 0, 12));
		centerPanel.add(centerButton);
		centerButton.setBounds(10, 10, 60, 30);
		
		getContentPane().add(centerPanel);
		centerPanel.setBounds(65, 35, 80, 50);
		
		closeButton.setText("Close");
		closeButton.setFont(new Font("Arial", 0, 12));
		getContentPane().add(closeButton);
		closeButton.setBounds(180, 140, 80, 25);
		
		jLabel1.setText("Set translation:");
		getContentPane().add(jLabel1);
		jLabel1.setBounds(230, 30, 85, 25);
		
		stepTextField.setBackground(new java.awt.Color(0, 0, 0));
		stepTextField.setForeground(new java.awt.Color(0, 255, 0));
		stepTextField.setText("1");
		stepTextField.setCaretColor(new java.awt.Color(0, 255, 0));
		getContentPane().add(stepTextField);
		stepTextField.setBounds(320, 30, 70, 25);
		
		scaleButton.setText("SetScale");
		scaleButton.setAlignmentY(0.0F);
		scaleButton.setIconTextGap(0);
		scaleButton.setMargin(new Insets(1, 1, 1, 1));
		scaleButton.setFont(new Font("Arial", 0, 12));
		getContentPane().add(scaleButton);
		scaleButton.setBounds(245, 60, 70, 25);
		
		scaleTextField.setBackground(new java.awt.Color(0, 0, 0));
		scaleTextField.setForeground(new java.awt.Color(0, 255, 0));
		scaleTextField.setText("1");
		scaleTextField.setCaretColor(new java.awt.Color(0, 255, 0));
		getContentPane().add(scaleTextField);
		scaleTextField.setBounds(320, 60, 70, 25);
		
		jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
		getContentPane().add(jSeparator1);
		jSeparator1.setBounds(220, 0, 20, 130);
		
		getContentPane().add(jSeparator2);
		jSeparator2.setBounds(0, 130, 470, 10);
		
		
		upButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						up();
					}
				});
		
		downButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						down();
					}
				});
		
		leftButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						left();
					}
				});
		
		rightButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						right();
					}
				});
		
		centerButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						center();
					}
				});
		
		closeButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						close();
					}
				});
		
		scaleButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						scale();
					}
				});
		
		
	}// </editor-fold>                        
	
	public void close(){
		this.dispose();
	}
	
	public void up(){
		this.translation = new Translation(0,this.stepTextField.getValue(),0);
		((PostpViewState)parent.getView().getViewState()).getGeoTransformViewState().getGeoTransform().addTransformation(translation);
		parent.getView().getController().compose();
		parent.getView().getDrawingArea().repaint();
	}
	
	public void down(){
		this.translation = new Translation(0,-this.stepTextField.getValue(),0);
		((PostpViewState)parent.getView().getViewState()).getGeoTransformViewState().getGeoTransform().addTransformation(translation);
		parent.getView().getController().compose();
		parent.getView().getDrawingArea().repaint();
	}
	
	public void left(){
		this.translation = new Translation(-this.stepTextField.getValue(),0,0);
		((PostpViewState)parent.getView().getViewState()).getGeoTransformViewState().getGeoTransform().addTransformation(translation);
		parent.getView().getController().compose();
		parent.getView().getDrawingArea().repaint();
	}
	
	public void right(){
		this.translation = new Translation(this.stepTextField.getValue(),0,0);
		((PostpViewState)parent.getView().getViewState()).getGeoTransformViewState().getGeoTransform().addTransformation(translation);
		parent.getView().getController().compose();
		parent.getView().getDrawingArea().repaint();
	}
	
	public void center(){
		parent.getView().getController().fit();
		parent.getView().getController().compose();
		parent.getView().getDrawingArea().repaint();
	}
	
	public void scale(){
		ScaleTransform scale = new ScaleTransform(this.scaleTextField.getValue(),this.scaleTextField.getValue(),this.scaleTextField.getValue());
		((PostpViewState)parent.getView().getViewState()).getGeoTransformViewState().getGeoTransform().addTransformation(scale);
		parent.getView().getController().compose();
		parent.getView().getDrawingArea().repaint();
	}
	
	/**
	 * Sets the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMaximumFractionDigits(3);
	}
	
	
	
//	/**
//	* @param args the command line arguments
//	*/
//	public static void main(String args[]) {
//	java.awt.EventQueue.invokeLater(new Runnable() {
//	public void run() {
//	new TranslationDialog().setVisible(true);
//	}
//	});
//	}
//	
	
}


